package com.quanyan.operative.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.operative.model.req.ReqOperatingActivity;
import com.quanyan.operative.model.resp.RespOperatingActivitySubject;
import com.quanyan.operative.service.OperateActivityService;
import com.quanyan.operative.service.impl.TbUserInfoServiceImpl;
import com.quanyan.place.client.PlaceClient;
import org.apache.commons.collections.MapUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 运营场馆活动Controller
 * Created by admin on 2016/11/22.
 */
@RestController
@RequestMapping(value = "/api/operative/place")
public class PlaceOperateController {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(PlaceOperateController.class);

    @Autowired
    private OperateActivityService activityService;

    @Autowired
    private PlaceClient placeClient;

    @RequestMapping(value = "getPlaceList")
    public APIResponse<List<RespOperatingActivitySubject>> getPlaceList(@RequestBody ReqOperatingActivity reqOperatingActivity){
        List<RespOperatingActivitySubject> respBlessingUserList = activityService.getPlaceList(reqOperatingActivity);
        return APIResponse.returnSuccess(respBlessingUserList);
    }


    /**
     * 获取专题及相关活动
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "getSubjectAndActivity")
    public APIResponse<?> getSubjectAndActivity(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer subjectId = MapUtils.getInteger(map, "subjectId"); // 专题ID
        Integer cityId = MapUtils.getInteger(map, "cityId");
        Integer pageNum = MapUtils.getInteger(map, "pageNum", Constants.DEFAULT_PAGE_NUM);
        Integer pageSize = MapUtils.getInteger(map, "pageSize",Constants.DEFAULT_PAGE_SIZE);
        RespOperatingActivitySubject result = activityService.getSubjectAndActivity(subjectId,cityId,pageNum,pageSize);
        if (null == result){
            return APIResponse.returnFail("专题不存在");
        }
        return APIResponse.returnSuccess(result);
    }


    /**
     * 判断用户是否有订场行为
     *
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/isUserBookPlace", method = RequestMethod.POST, produces = "application/json")
    public APIResponse isUserBookPlace(@RequestBody Map map, BindingResult bindingResult,HttpServletRequest request){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        Integer uid = (Integer)request.getAttribute("uid"); //用户id

        logger.info("用户uid:{}",uid);

        if (uid == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        return activityService.isUserBookPlace(uid);
    }

}
